import { Game, game } from "cc"
import { ServerConfig } from "../../../src/config/ServerConfig"
import { GameSocketManager } from "../../../src/engine/GameSocketManager"
import { Logger } from "../../../src/engine/Logger"
import { UIManager } from "../../../src/engine/UIManager"
import { ProtoManager, WsSendData } from "../../../src/lib/net/protobuf/ProtoManager"

import proto from "../../../src/lib/net/protobuf/jhaoproto.js"
import { ViewTip } from "../../../src/view/ViewTip"
import { AccountModel } from "../login/model/AccountModel"
import { UserModel } from "../model/UserModel"
import ServerTimer from "../../../src/engine/ServerTimer"
import { EventManager } from "../../../src/engine/EventManager"
import { EngineConfig } from "../../../src/engine/EngineConfig"
import VersionManager from "../../../src/engine/VersionManager"
import { ViewHall } from "../hall/view/ViewHall"



export class HallSocketController {

    private static _instance: HallSocketController;

    public socketUrl: string = "";
    private lastHideTime: number = 0; //最后隐藏时间 

    public static get instance(): HallSocketController {
        if (!HallSocketController._instance) {
            HallSocketController._instance = new HallSocketController();
            HallSocketController._instance.init();
        }
        return HallSocketController._instance;
    }


    public init() {
        game.off(Game.EVENT_HIDE, this.onHideToBackground, this);
        game.off(Game.EVENT_SHOW, this.aWakeFromBackGround, this);
        game.on(Game.EVENT_HIDE, this.onHideToBackground, this);
        game.on(Game.EVENT_SHOW, this.aWakeFromBackGround, this);
        EventManager.instance.clear(this);
        EventManager.instance.on(EngineConfig.Event_Type.Event_Socket_Connected, this.onConnected, this);
    }

    private onConnected(isNeedReconnect: boolean) {
        if (isNeedReconnect) {
            this.login(false)
        }
    }

    /**
    * 隐藏程序
    */
    private onHideToBackground() {
        this.lastHideTime = ServerTimer.now;
    }

    /**
     *  从后台唤醒
     */
    private aWakeFromBackGround() {
        if (UserModel.instance.isLogin) {
            // if (RoomModel.instance.isPlayingGame) {
            //     if ((ServerTimer.now - this.lastHideTime) >= 3 * 1000) {
            //         this.closeSocket()
            //     }
            // } else {
            if ((ServerTimer.now - this.lastHideTime) >= 5 * 1000) {
                GameSocketManager.instance.getSocketClient(this.socketUrl).closeSocket()
            }
            // }
        }
    }


    //socket 登录 
    public login(needGotoHall: boolean, callback: Function = null) {
        this.socketUrl = ServerConfig.getSocketUrl();
        if (!UserModel.instance.isLogining && !UserModel.instance.isLogin) {
            UIManager.instance.showLoadingIcon()
            UserModel.instance.isLogining = true;
            let send_data: proto.JhaoProto.IReqLogin = {
                account: AccountModel.instance.account,
                token: AccountModel.instance.token,
                nickname: AccountModel.instance.nickname,
                sex: AccountModel.instance.sex,
                headimgurl: AccountModel.instance.headimgurl
            }
            Logger.log("login req==send_data=", needGotoHall, send_data)
            let send_obj: WsSendData = {
                cmd_index: proto.JhaoProto.CmdIndex.Login,
                en_proto_ctor: proto.JhaoProto.ReqLogin,
                data: send_data,
                callback: (respData: proto.JhaoProto.BaseMsg) => {
                    UserModel.instance.isLogining = false;
                    UIManager.instance.hideLoadingIcon()
                    Logger.log("login resp==data=", respData)
                    if (respData.errorCode == proto.JhaoProto.ErrorCodeWs.Suc) {
                        let respLogin: proto.JhaoProto.RespLogin = ProtoManager.instance.decode(proto.JhaoProto.RespLogin, respData.data)
                        UserModel.instance.isLogin = true
                        UserModel.proto2Model(respLogin.userInfo, UserModel.instance)
                        Logger.log("login resp==UserModel=", UserModel.instance)
                        callback && callback()
                        if (needGotoHall) {
                            UIManager.instance.showView(ViewHall.uiConfig, true, () => {
                                UIManager.instance.closeExceptView(ViewHall.uiConfig);
                            })
                        }
                    } else {
                        UIManager.instance.showTip(respData.errorMsg, ViewTip.TipType.Fail);
                    }
                }
            }
            GameSocketManager.instance.getSocketClient(this.socketUrl).sendToSocket(send_obj);
        }
    }

}